package com.iot.ht.controller.hterror;

import com.iot.core.annotation.Auth;
import com.iot.core.annotation.Log;
import com.iot.core.data.BasePageDtQto;
import com.iot.core.data.entity.HtError;
import com.iot.core.data.mapper.HtErrorMapper;
import com.iot.core.enums.AuthType;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import static com.iot.core.data.entity.table.HtErrorTableDef.HT_ERROR;

@Slf4j
@RestController
@RequestMapping("/hterror")
@Tag(name = "系统异常", description = "")
public class HtErrorController {
    @Autowired
    HtErrorMapper htErrorMapper;

    @Auth(authType = AuthType.Login)
    @Log(logNeed = false,logTemplete ="",logPin = true,logPout = true)
    @PostMapping("/page")
    @Operation(summary = "系统异常分页", description = "")
    public Page<HtError> page(@RequestBody BasePageDtQto qm) throws Exception {
        var qw=htErrorMapper.getQueryWrapper();
        if(StringUtils.hasText(qm.getDtb() )){
            qw.and( HT_ERROR.CRT_DT.ge(qm.getDB()));
        }

        if(StringUtils.hasText(qm.getDte() )){
            qw.and( HT_ERROR.CRT_DT.lt(qm.getDE()));
        }

        if(StringUtils.hasText(qm.getKey() )){
            if(StringUtils.hasText(qm.getKey() )){
                qw.and( HT_ERROR.HTDEPT_NAME.like(qm.getKey())
                        .or( HT_ERROR.NAME.like(qm.getKey()) )
                        .or( HT_ERROR.URL.like(qm.getKey()) )
                );
            }
        }

        var sort=qm.getSort();
        qw.orderBy( sort.toString()  );
        var r=htErrorMapper.page(  qm,qw );
        return r;


    }


}
